//
//  AppDelegate.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/11/22.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
//        QorumLogs
        QorumLogs.enabled = true
//        QorumLogs测试
//        QorumLogs.test()
//        QorumLogs设置打印的级别  1--Debug  2--Info  3--Warning  4--Error
//        测试中一般设置为1，  实际发布中设置为4
        QorumLogs.minimumLogLevelShown = 1
        QL3("实际开发中，重新设置minimumLogLevelShown = 4")
//        QorumLogs设置打印指定的文件的内容
//        QorumLogs.onlyShowThisFile(AppDelegate)
        
        QLShortLine()
        QL2(WHYUserAccountViewModel.sharedUserAccount.userAccount)
        QLShortLine()
        
//        设置AFN--当通过AFN发送网络请求时，会在状态栏显示菊花
//        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        

        
        setupAppearance()

        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = defaultViewController
        
        window?.makeKeyAndVisible()
        
        NSNotificationCenter.defaultCenter().addObserverForName(WHYWBSwitchViewControllerNotification,
            object: nil,
            queue: nil) { [weak self](notification) -> Void in
                let vc = notification.object != nil ? WHYWelcomeViewController() : WHYMainViewController()
                self?.window?.rootViewController = vc
        }
        
        return true
    }
    
//    应用程序进入后台时
    func applicationDidEnterBackground(application: UIApplication) {
//        清理缓存
        WHYStatusDAL.clearDataCache()
    }

//    设置导航栏全局外观
    private func setupAppearance () {
        UINavigationBar.appearance().tintColor = WHYWBAppearanceTintColor
        UITabBar.appearance().tintColor = WHYWBAppearanceTintColor
    }
    
    

}

extension AppDelegate {
    
    private var defaultViewController: UIViewController {
        if WHYUserAccountViewModel.sharedUserAccount.userLogon {
            return isNewVersion ? WHYNewFeatureViewController() : WHYWelcomeViewController()
        }
        
        return WHYMainViewController()
    }
    
    private var isNewVersion: Bool {
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)
        
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey("sandboxVersionKey")
        
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion!, forKey: "sandboxVersionKey")
        
        return currentVersion > sandBoxVersion
    }
    
}

